import java.util.Comparator;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;

public class Leet23 {
    public ListNode mergeKLists(ListNode[] lists) {
        if (lists.length==0)
            return null;
        Queue<ListNode> q = new PriorityQueue<>(Comparator.comparingInt(o -> o.val));
        for (ListNode list : lists) {
            while (list!=null){
                q.add(list);
                list = list.next;
            }
        }
        if (q.size()==0)
            return null;
        ListNode head = q.remove();
        ListNode re = head;
        while (!q.isEmpty()){
            head.next = q.remove();
            head = head.next;
        }
        head.next=null;
        return re;
    }
}
